package wolftrace_company.study_android_ui.activity;

import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;


import org.json.JSONException;
import org.json.JSONObject;

import java.io.IOException;

import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.FormBody;
import okhttp3.MediaType;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.RequestBody;
import okhttp3.Response;
import wolftrace_company.study_android_ui.R;

/**
 * Created by SimpleTonOne on 2018/4/1.
 */

public class SendDataActicity extends AppCompatActivity {
    private EditText editText1;
    private EditText editText2;
    private EditText editText3;
    private Button button1;
    private TextView textView;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);
        setContentView(R.layout.senddatapage);
        init();
        button1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                String title = editText1.getText().toString();
                String info = editText2.getText().toString();
                if (title.equals("") || info.equals("")) {
                    Toast.makeText(SendDataActicity.this, "输入的标题或者信息不能为空", Toast.LENGTH_LONG).show();
                } else {
                    try {
                        send(title, info);
                    } catch (JSONException e) {
                        e.printStackTrace();
                    }
                }
            }
        });
    }

    public void send(String title, String info) throws JSONException {
        Toast.makeText(SendDataActicity.this, "正在发送数据", Toast.LENGTH_LONG).show();
        OkHttpClient okHttpClient = new OkHttpClient();
        MediaType mediaType = MediaType.parse("application/json,charset=utf-8");
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("CarId",1);
        jsonObject.put("CarAction","Start");
        jsonObject.put("UserName","Z0004");
        Log.d("meg",jsonObject.toString());
        RequestBody requestBody = RequestBody.create(mediaType, jsonObject.toString());
        final Request request = new Request.Builder().post(requestBody).url("http://192.168.1.103:8890/transportservice/action/SetCarMove.do").build();
        okHttpClient.newCall(request).enqueue(new Callback() {
            @Override
            public void onFailure(Call call, IOException e) {
                Log.d("msg", "提交数据失败");
            }

            @Override
            public void onResponse(Call call, Response response) throws IOException {
                if (response.isSuccessful()) {
                    String str = response.body().string();
                    Log.d("meg", str);
                    Message message = new Message();
                    message.what = 2;
                    message.obj = str;
                    handler.sendMessage(message);

                } else {
                    Toast.makeText(SendDataActicity.this, "提交1数据失败了", Toast.LENGTH_LONG).show();
                }
            }
        });
    }

    public void init() {
        editText1 = findViewById(R.id.et1);
        editText2 = findViewById(R.id.et2);
        editText3 = findViewById(R.id.et3);
        button1 = findViewById(R.id.sendPage_btn);
        textView = findViewById(R.id.show_info);
    }

    Handler handler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            if (msg.what == 2) {
                String str = (String) msg.obj;
                Log.d("str", str);
                try {
                    JSONObject jsonObject = new JSONObject(str);
                    String a = jsonObject.getString("ERRMSG");
                    String b = jsonObject.getString("RESULT");
                    textView.setText("传输回来的结果代码：" + a + "\n传输回来的结果内容：" + b);
                } catch (JSONException e) {
                    e.printStackTrace();
                }


            }
        }
    };
}
